package models

import (
	"gorm.io/gorm"
	"time"
)

type UserBasic struct {
	gorm.Model
	UserName      string       `gorm:"column:user_name;type:varchar(50);not null;index" json:"user_name"`
	NickName      string       `gorm:"column:nick_name;type:varchar(255);not null" json:"nick_name"`
	Password      string       `gorm:"column:password;type:varchar(255);not null" json:"password"`
	Phone         string       `gorm:"column:phone;type:varchar(255);not null;" json:"phone"`
	Email         string       `gorm:"column:email;type:varchar(100);not null;" json:"email"`
	Header        string       `gorm:"column:header;type:varchar(510)" json:"header"`
	ClientIP      string       `gorm:"column:client_ip;type:varchar(45)" json:"client_ip"`
	ClientPort    string       `gorm:"column:client_port;type:varchar(10)" json:"client_port"`
	LoginTime     time.Time    `gorm:"column:login_time;type: timestamp;" json:"login_time"`
	LoginOutTime  time.Time    `gorm:"column:logout_time;type: timestamp;" json:"login_out_time"`
	HeartbeatTime uint64       `gorm:"column:heartbeat_time;type:bigint unsigned" json:"heartbeat_time"`
	DeviceInfo    string       `gorm:"column:device_info;type:text" json:"device_info"`
	GitHubID      int          `gorm:"column:github_id;type:int" json:"githubId"`
	Groups        []GroupModel `gorm:"many2many:user_groups;foreignKey:ID;joinForeignKey:UserId;References:ID;joinReferences:GroupId" json:"groups"`
	Friends       []UserBasic  `gorm:"many2many:friends;foreignKey:ID;joinForeignKey:UserId;References:ID;joinReferences:FriendId;" json:"friends"`
}

/*
 *
 * 好友关系表，保留双向两条记录
 */
type UserFriends struct {
	gorm.Model
	UserId   uint   `gorm:"not null;index:idx_user_friend;comment:用户ID" json:"user_id"`     // 当前用户 ID
	FriendId uint   `gorm:"not null;index:idx_user_friend;comment:好友用户ID" json:"friend_id"` // 好友 ID
	Remark   string `gorm:"type:varchar(100);comment:好友备注" json:"remark"`                   // 用户对好友的备注
	Status   int    `gorm:"type:tinyint;default:1;comment:关系状态：1正常 2拉黑 3删除" json:"status"`  // 好友状态
}

func (u UserFriends) TableName() string {
	return "friends"
}

type FriendRequest struct {
	gorm.Model
	SenderId   uint   `gorm:"not null;index:idx_sender_receiver;comment:发起人用户ID" json:"sender_id"`
	ReceiverId uint   `gorm:"not null;index:idx_sender_receiver;comment:接收人用户ID" json:"receiver_id"`
	Status     int    `gorm:"type:tinyint;default:0;comment:申请状态：0待处理 1已同意 2已拒绝 3已过期" json:"status"`
	Message    string `gorm:"type:varchar(255);comment:附加消息" json:"message"`
}

func (u FriendRequest) TableName() string {
	return "friend_requests"
}

// TableName 设置表名
func (u UserBasic) TableName() string {
	return "user_info"
}
func (u *UserBasic) Create(db *gorm.DB) error {
	return db.Create(u).Error
}

func (u *UserBasic) GetByID(db *gorm.DB, id uint) error {
	return db.First(u, id).Error
}

func (u *UserBasic) DeleteById(db *gorm.DB, id int) error {
	u.ID = uint(id)
	return db.Delete(u).Error
}
